package com.kotlin.hxh.adapter

import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import android.widget.TextView
import com.kotlin.hxh.R
import com.kotlin.hxh.entity.Article
import com.kotlin.hxh.listener.OnItemClickListener
import java.util.*

/**
 * @author huxh
 * @date 2018/12/20.
 */
class HomeAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<RecyclerViewHolder>() {
    private val mItems = ArrayList<Article>()
    private var mClickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int) = RecyclerViewHolder.Companion.createViewHolder(p0.context, p0, R.layout.item_home)

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        holder.itemView.setOnClickListener { mClickListener?.onItemClick(holder.adapterPosition, it, holder) }
        val item = mItems[position]
        holder.getView<TextView>(R.id.tv_title).text = item.title
        holder.getView<TextView>(R.id.tv_content).text = item.content
    }

    fun setDatas(items: List<Article>) {
        mItems.clear()
        mItems.addAll(items)
    }

    fun getItem(position: Int) = mItems[position]

    override fun getItemCount() = mItems.size
    fun setOnItemClickListener(listener: OnItemClickListener) {
        mClickListener = listener
    }
}